package cn.edu.qlnu.xky.test.controller;

import cn.edu.qlnu.xky.test.model.Borrow;
import cn.edu.qlnu.xky.test.model.User;
import cn.edu.qlnu.xky.test.service.BorrowService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import cn.edu.qlnu.xky.test.common.MsgExchage;
import cn.edu.qlnu.xky.test.model.Book;
import cn.edu.qlnu.xky.test.service.BookService;

/**
 * Servlet implementation class TestController
 * 和页面jsp及service进行上传下达，通信
 * 把页面传递的参数组合成对象传递给service
 * 将service传回的参数传递给页面
 */
public class BookController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("我是属于BookController里面的doGet方法");
		
		// flag为标志，add添加，search为查询
		String flag = request.getParameter("flag");
		String url = "/book/search.jsp";
		System.out.println("flag=" + flag);
		// System.out.println("id="+request.getParameter("id"));
		if (flag == null) {
			url = "/book/add.jsp";
		} else {
			BookService service = new BookService();
			if (flag.equals("add")) {

				// 获取添加图书信息的代码
				String bookName = request.getParameter("bookName");
				String author = request.getParameter("author");

				String putTime = request.getParameter("putTime");
				String count = request.getParameter("count");
				String isbn = request.getParameter("isbn");
				
				// 下面代码是封装这些信息，然后提交到后台，保存，返回结果
				int ret = 0;
				Book b = new Book();
				b.setBookName(bookName);
				b.setAuthor(author);
				b.setPubTime(MsgExchage.stringToDate(putTime));
				b.setIsbn(isbn);
				b.setCount(Integer.parseInt(count));
				ret = service.addBook(b);
				
				String msg = "";
				if(ret == 1) {
					msg = "图书添加成功";
				}else {
					msg = "图书添加失败";
				}
				request.setAttribute("msg", msg);
				url = "/book/search.jsp";
			} else if (flag.equals("search")) {
				String bookName = request.getParameter("bookName");
				String pageNo = request.getParameter("pageNo");
				if(bookName == null) {
					bookName = "";
				}
				request.setAttribute("page", service.searchByNamePage(bookName,Integer.parseInt(pageNo)));

				url = "/book/search.jsp";
			} else if (flag.equals("edit")) {
				// 修改前的查询
				String id = request.getParameter("id");
				System.out.println("id=" + id);
				// 模拟根据id查询该条学生信息
				Book b = service.searchById(Integer.parseInt(id));
				request.setAttribute("book", b);
				url="/book/edit.jsp";
			}else if(flag.equals("doEdit")) {
				//真正要修改
				// 获取图书信息的代码
				String bookName = request.getParameter("bookName");
				
				String id = request.getParameter("id");
				String author = request.getParameter("author");
				String pubTime = request.getParameter("pubTime");
				
				String count = request.getParameter("count");
				String isbn = request.getParameter("isbn");
				
				Book b = new Book();
				
				b.setBookName(bookName);
				b.setId(Integer.parseInt(id));
				b.setAuthor(author);
				b.setPubTime(MsgExchage.stringToDate(pubTime));
				b.setIsbn(isbn);
				b.setCount(Integer.parseInt(count));
				
				int ret = 0;
				if (bookName != null && !bookName.trim().equals("")) {
					ret = service.editBook(b);
				}
				request.setAttribute("ret", ret);
				String msg = "";
				if(ret == 0) {
					msg = "修改失败";
				}else {
					msg="修改成功";
				}
				request.setAttribute("msg", msg);
				url = "/book/search.jsp";
			}else if(flag.equals("delete")) {
				//删除
				String id = request.getParameter("id");
				int ret = service.deleteBook(Integer.parseInt(id));//0;//service.deleteStudent(Integer.parseInt(id));
				String msg = "";
				if(ret == 0) {
					msg = "删除失败";
				}else {
					msg="删除成功";
				}
				request.setAttribute("msg", msg);
				url = "/book/search.jsp";
			}else if(flag.equals("borrow")) {
				String bookId = request.getParameter("id");
				Book b = service.searchById(Integer.parseInt(bookId));
				String msg = "";
				if (b.getCount()<=0) {
					msg = "借阅失败, 图书库存不足";
				}else {
					HttpSession session = request.getSession(false);
					User user = (User) session.getAttribute("curUser");
					Borrow borrow = new Borrow();
					borrow.setBookId(Integer.parseInt(bookId));
					borrow.setUserId(user.getId());
					Date borrowDate = new Date();
					borrow.setBorrowDate(borrowDate);
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(borrowDate);
					calendar.add(Calendar.DAY_OF_MONTH, 10); // 负数表示减天数
					Date dueDate = calendar.getTime();
					borrow.setDueDate(dueDate);
					BorrowService borrowService = new BorrowService();
					int ret = borrowService.borrowBook(borrow);
					if (ret == 0) {
						msg = "借阅图书失败";
					}else {
						msg = "借阅成功";
						service.updateBookCount(Integer.parseInt(bookId), "sub");
					}
				}
				request.setAttribute("msg", msg);
				url = "/bookBorrow/search.jsp";
			}
		}

		request.getRequestDispatcher(url).forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
